package com.example.academic_affairs_system.mapper.provider;

import cn.hutool.core.util.StrUtil;
import com.example.academic_affairs_system.model.dto.classStudent.ClassStudentPageDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * @author hjx
 * @since 2023/02/03 11:10 PM
 */
public class AasClassStudentProvider {
  public String getClassStudentVOByClassId(@Param("dto") ClassStudentPageDTO dto) {
    return new SQL() {
      {
        SELECT(
                "username as studentNumber",
                "student_id as userId",
                "acs.class_id as classId",
                "au.nickname",
                "au.id_card as idCard",
                "au.age",
                "if(au.sex = 1, '男', '女') as sex",
                "au.`concat` as `concat`",
                "au.family_concat as familyConcat",
                "au.origin",
                "au.address",
                "au.last_time as lastTime",
                "acs.`position`",
                "acs.position_desc as `positionDesc`",
                "acs.rating")
            .FROM("aas_class_student acs")
            .LEFT_OUTER_JOIN("aas_user au on acs.student_id = au.id");
        // 根据负责人搜索
        if (dto.getDirectorId() != null) {
          LEFT_OUTER_JOIN("aas_class ac on ac.id = acs.class_id ");
          WHERE("ac.directors_user_id = #{dto.directorId}");
        }
        // 根据班级 id 搜索
        if (dto.getId() != null) {
          WHERE("acs.class_id = #{dto.id}");
        }
        // 根据姓名或学号进行查找
        if (StrUtil.isNotBlank(dto.getKeyword())) {
          AND()
              .WHERE(
                  "au.nickname like concat('%',#{dto.keyword},'%') or au.username like concat('%',#{dto.keyword},'%')");
        }
        WHERE("acs.enabled = 1 and au.enabled = 1");
      }
    }.toString();
  }


}
